<?php
include '../../../wp-blog-header.php';
$city = $argv[1];
$url = 'http://www.cityoffers.in/'.$city.'/deals/alldeals.xml';

if($city == 'delhi-ncr'){
  $city = 'delhi';
}
$brand_id = '796';

function download_page($path){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$path);
        curl_setopt($ch, CURLOPT_FAILONERROR,1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        $retValue = curl_exec($ch);                      
        curl_close($ch);
        return $retValue;
}

$curl_json_output = download_page($url);
$affilired_deals = xml2array($curl_json_output);
 
                                                    
 foreach ($affilired_deals['response']['deals']['deal'] as $key=>$value){

  $deal_link = $value['deal_url'];
  $name = substr($value['title'],0,120);
  //$deal_title = $value['title'];
  $image = $value['large_image_url'];
  $start = date('Y-m-d H:i:s',strtotime($value['start_date']));
  $end = date('Y-m-d H:i:s',strtotime($value['end_date']));
  $orig = explode ( '.', $value['value']);
  $new = explode ( '.', $value['price']);
  $discount = str_replace('%','',$value['discount_percent']);

    /*
    echo '<pre>';
    print_r($value);
    echo '</pre>';
    exit();
    */
      
    $columns_deal['name'] = pods_sanitize(ucwords(strtolower($name)));
    $columns_deal['camp_link'] = substr(sanitize_title($columns_deal['name']),0,128);
    
    $deal = new Pod('deal');
    $deal->page=1;
    $deal->search=false;
    $where = "t.camp_link = '".$columns_deal['camp_link']."' AND t.daily = 1";
    $deal->findRecords('id DESC', 1,$where);
    $dealsFound = $deal->getTotalRows();
    
    if($dealsFound == 0)
    {
      $city_pod = new Pod('city');
      $city_pod->page=1;
      $city_pod->search=false;
    	$city_pod->findRecords("name ASC",1,"t.name = '$city'");
    	while($city_pod->fetchRecord())
    	{
      	$city_id  = $city_pod->get_field('id');
    	}
      $columns_deal['city'] = $city_id;
      
      $shortened_url = make_bitly_url($deal_link);
      if(empty($shortened_url))
      {
        $shortened_url = make_goog_url($deal_link);
      }
      $columns_deal['extlink'] = $shortened_url;
      
      $columns_deal['camp_end'] = $end;
      $columns_deal['camp_start'] = $start;
      $columns_deal['brand'] = $brand_id;
      $columns_deal['daily'] = '1';
      $columns_deal['oriprice'] = $orig[1];
      $columns_deal['disprice'] = $new[1];
    
      $columns_deal['disper'] = $discount;
      
      $columns_deal['ext_image_url'] = $image;
    
      $cat_id = check_daily_category($columns_deal['name']);
      if(!empty($cat_id))
      {
        $columns_deal['camp_cat'] = $cat_id;
      }
      
      $api = new PodAPI();
      $params = array('datatype' => 'deal', 'columns' => $columns_deal);
    
      
      echo '<pre>';
      print_r($params);
      echo '</pre>';
       
      $api->save_pod_item($params);
      }
}  
?>